<!-- 排班管理 -->
<template>
  <div class="dashboard-container transport-task customer-list-box">
    <!-- 顶部搜索 -->
    <el-card>
      <el-form
        :inline="true"
        :model="selectForm"
        class="demo-form-inline"
      >
        <el-form-item
          label="员工账号:"
          style="margin-left:50px"
        >
          <el-input
            v-model="selectForm.employeeNumber"
            placeholder="员工账号"
            style="width: 300px;"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="员工姓名:"
          style="margin-left:50px"
        >
          <el-input
            v-model="selectForm.name"
            placeholder="员工姓名"
            style="width: 300px;"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="工作模式:"
          style="margin-left:50px"
        >
          <el-select
            v-model="selectForm.workPatternId"
            style="width: 300px;"
          >
            <el-option
              v-for="(item, index) in pattern"
              :key="index"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <br>
        <el-form-item
          label="月份："
          style="margin-left:50px"
        >
          <el-date-picker
            v-model="selectForm.month"
            type="month"
            placeholder="选择月"
            style="width: 300px;"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item
          label="所属机构:"
          style="margin-left:50px"
        >
          <el-select
            v-model="selectForm.agentId"
            placeholder="所属机构"
            style="width: 300px;"
          >
            <el-option
              v-for="(item, index) in workArr"
              :key="index"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item style="margin-left:70px">
          <el-button
            type="danger"
            @click="handleSelect"
          >搜索</el-button>
        </el-form-item>
        <el-form-item>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 列表渲染 -->
    <el-card style="margin-bottom:55px">
      <el-button
        type="danger"
        @click="$router.push('workArrange-setting')"
      >排班设置</el-button>
      <el-button
        class="bind"
        @click="handleBind"
      >绑定排班</el-button>
      <el-table
        :data="workList"
        stripe
        style="width: 100%;margin-top:20px"
        @selection-change="tableSelect"
      >
        <el-table-column
          type="selection"
          width="55"
        >
        </el-table-column>
        <el-table-column
          prop="employeeNumber"
          label="员工账号"
          width="110"
        >
        </el-table-column>
        <el-table-column
          prop="name"
          label="司机姓名"
          width="110"
        >
        </el-table-column>
        <el-table-column
          prop="phone"
          label="司机电话"
          width="110"
        >
        </el-table-column>
        <el-table-column
          prop="userType"
          label=" 员工角色"
          width="110"
          :formatter="formateUserType"
        >
        </el-table-column>
        <el-table-column
          prop="workPatternName"
          label="当前工作状态"
          width="110"
        >
        </el-table-column>

        <el-table-column
          v-for="index in 31"
          :key="index"
          :prop="String(index - 1)"
          :label="String(index)"
        >
          <template #default="{row}">
            <span :style="getNameStyle(row[index])">{{ row[index] }}</span>
          </template>
        </el-table-column>

      </el-table>
      <div class="block">
        <span class="demonstration"></span>
        <el-pagination
          :current-page="page"
          :page-sizes="[8, 10, 13, 15]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        >
        </el-pagination>
      </div>
    </el-card>

    <!-- 绑定排版弹窗 -->
    <el-dialog
      title="人工调整"
      :visible.sync="dialogVisible"
      width="30%"
    >
      <span>
        <div style="text-align: center;">
          <el-radio
            v-model="radio"
            label="1"
          >礼拜制</el-radio>
          <el-radio
            v-model="radio"
            label="2"
          >连续制</el-radio>
        </div>
        <div style="display:flex;margin:20px 0">
          <div style="width: 100px;line-height:40px">模式名称</div>
          <el-select
            v-model="value1"
            placeholder="请选择"
          >
            <el-option
              v-for="item in pattern"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </div>
        <div>
        </div>
      </span>
      <span
        slot="footer"
        class="dialog-footer"
      >
        <div style="text-align: center;"><el-button @click="dialogVisible = false">取 消</el-button>
          <el-button
            type="danger"
            @click="confirmBind"
          >确 定</el-button>
        </div>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { getWorkAPI, getWorkListAPI, bindWorkAPI } from '@/api/work'
import { agencyList } from '@/api/institutions'

export default {
  data() {
    return {
      page: 1,
      pageSize: 10,
      total: 0,
      workList: [],
      pattern: [],
      selectForm: {},
      value2: '', // 搜索月
      workArr: [],
      selectBind: {}, // 选择绑定
      dialogVisible: false,
      radio: '',
      value1: ''
    }
  },
  mounted() {
    this.getWorkPage()
    this.getWorkList()
  },
  methods: {
    // 确定绑定
    async confirmBind() {
      this.selectBind.workPatternType = this.radio
      this.selectBind.workPatternId = Number(this.value1)
      const res = await bindWorkAPI(this.selectBind)
      if (res.code === 1) this.$message.error(res.msg)
      this.dialogVisible = false
    },
    // 处理打开绑定排班弹窗
    handleBind() {
      this.dialogVisible = true
    },
    // 设置样式
    getNameStyle(row) {
      // 根据不同条件返回不同的样式
      if (row === '上') {
        return {
          background: '#FFF3EF',
          border: '1px solid rgba(225,85,54,1)',
          color: '#E15536',
          borderRadius: '50%'
        }
      } else {
        return {
          background: '#F0F0F0',
          border: '1px solid #65546c',
          color: '#818693',
          borderRadius: '50%'
        }
      }
    },
    // 查看表格现在选中
    tableSelect(selection) {
      this.selectBind = {}
      if (selection[0]) this.selectBind.userType = String(selection[0].userType)
      const arr = []
      selection.forEach(item => {
        arr.push(Number(item.id))
      })
      this.selectBind.userIdList = arr
    },
    // 格式化员工类型
    formateUserType(row) {
      const MAP = {
        1: '员工',
        2: '快递员',
        3: '司机'
      }
      return MAP[row.userType]
    },
    // 搜索
    handleSelect() {
      this.page = 1
      this.pageSize = 10
      this.getWorkPage()
    },
    // 重置
    handleReset() {
      this.page = 1
      this.pageSize = 10
      this.selectForm = { }
      this.getWorkPage()
    },
    // pageSize变化时触发
    handleSizeChange(current) {
      this.pageSize = current
      this.getWorkPage()
    },
    // currentPage 改变时会触发
    handleCurrentChange(current) {
      this.page = current
      this.getWorkPage()
    },
    // 获取分页数据
    async getWorkPage() {
      const res = await getWorkAPI({ page: this.page, pageSize: this.pageSize, ...this.selectForm })
      res.data.items.forEach(item => {
        // console.log(item)
        item.workSchedules.forEach((ele, index) => {
          if (ele === true) item[index] = '上'
          if (ele === false) item[index] = '休'
        })
      })
      this.workList = res.data.items
      this.total = Number(res.data.counts)
    },
    // 获取工作列表 和所属机构
    async getWorkList() {
      const res = await getWorkListAPI()
      const arr = Object.values(res.data)
      const newArr = []
      arr.forEach(item => {
        newArr.push(...item)
      })
      this.pattern = newArr
      const res1 = await agencyList()
      const data = JSON.parse(res1.data)
      this.workArr = data
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
::v-deep .el-date-editor--month {
  width: 300px !important;
}
::v-deep .el-form-item__label {
  width: 90px;
}
.block {
  text-align: center;
  margin-top: 30px;
}
::v-deep .el-card {
  margin: 15px 20px;
}
::v-deep th.date{
  .cell{
    padding-left: 17px!important;
  }
}
.bind:hover {
  border: 1px solid #ed8b76;
  background-color: #fee7e2;
  color: #e15536;
}

.bind{
  border: 1px solid #ed8b76;
  background-color: #fee7e2;
  color: #e15536;
}

.bind:focus {
  border: 1px solid #ed8b76;
    background-color: #fee7e2;
    color: #e15536;
}
.work {
  width: 24px;
  height: 24px;
  background: #ffeeeb;
  border: 1px solid #e15536;
  border-radius: 50%;
  text-align: center;
  color: #e15536;
  font-size: 13px;
}
.history-name {
  width: auto !important;
  ::v-deep .el-input__inner {
    width: 293px;
  }
}

.workArrange-manage-people-setting {
  ::v-deep .el-dialog__body {
    padding: 30px 50px 10px 50px !important;
  }
  ::v-deep .el-dialog__footer {
    padding-bottom: 40px;
  }
}

.work-type {
  ::v-deep .el-form-item__content {
    margin-left: 40px !important;
  }
  ::v-deep .el-radio:first-child {
    margin-right: 80px !important;
  }
  ::v-deep.el-radio__label {
    font-weight: bold;
    color: #333333;
  }
}

.dialog-footer {
  .el-button {
    width: 89px;
    height: 36px;
    padding: 10px 20px;
  }
}

.rest {
  width: 24px;
  height: 24px;
  background: #eff3f8;
  border: 1px solid #d8dde3;
  border-radius: 50%;
  text-align: center;
  color: #20232a;
  font-size: 13px;
}

.alert {
  margin: 10px 0px;
}
.pagination {
  margin-top: 40px;
}
.transport-task ::v-deep .el-table td,
.el-table th {
  padding: 12px 0;
  min-width: 0;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  text-overflow: ellipsis;
  vertical-align: middle;
  position: relative;
  text-align: left;
  overflow: hidden;
}

// 下拉框
::v-deep .el-select {
  width: 100%;
}

/*定义滚动条轨道 内阴影+圆角*/
::v-deep .el-table--scrollable-x .el-table__body-wrapper {
  padding-bottom: 5px;
  margin-bottom: 5px;
  &::-webkit-scrollbar {
    height: 10px;
  }

  /*定义滑块 内阴影+圆角*/
  &::-webkit-scrollbar-thumb {
    border-radius: 10px;
    background-color: rgba(144, 147, 153, 0.3);
  }

  &::-webkit-scrollbar-track-piece {
    margin-right: 3px;
    margin-left: 3px;
  }
}
::v-deep .el-card{
  overflow: initial;
}
</style>

<style></style>
